

*** load data
use "sop2021JLCdata.dta"


 
*** set data as time series
tsset term



******************
******************
****************** 
*** MED NB REG ***
******************
******************
******************

*** nbreg estimate with bootstrap se 
nbreg totalGrantedFJC totalGrantedFJC_t1 post1988 diff_numCertPool white medDist bindergrid1_t5 diff_absAvgCircSCdist diff_totalCertPet diff_pcthighestcoalition_t1 diff_pctDisconnected_t1
*, vce(bootstrap,rep(5000) seed(31690)) 


*** store estimates
est store mednb
predict phatmednb
predict xbmednb, xb


*** obtain summary fit measures of the full model (fit1)
fitstat, saving(mednbfit)



***********************************************************
*** BAR PLOT ESTIMATES FOR UNCONSTRAINED COUNTERFACTUAL ***
***********************************************************

*** set up backup vfpDist
gen medDist_bu = .
replace medDist_bu = medDist


*** set vfpDist at 0 = unconstrained Court
replace medDist = 0


*** get  of socecon at min
predict phatmedDist0


*** replace vfpDist with saved backup
replace medDist = medDist_bu


*** gen pred difference variable
gen medDist_predDiff = .
replace medDist_predDiff = phatmedDist0 - phatmednb 


*** gen observed difference variable
gen medDist_obsDiff = .
replace medDist_obsDiff = phatmedDist0 - totalGrantedFJC


*** gen total sum var or predictions
gen medDistpred_sum = .
replace medDistpred_sum = sum(medDist_predDiff)


*** gen total sum var of obs/pred diff
gen medDistobs_sum = .
replace medDistobs_sum = sum(medDist_obsDiff)


*** drop backup
drop medDist_bu


*************************************************************
*** BAR PLOT ESTIMATES FOR CONSTRAINED COUNTERFACTUAL 99% ***
*************************************************************


est restore mednb


*** set up backup vfpDist
gen medDist_bu = .
replace medDist_bu = medDist


*** examine medDist (95% = .4743478, 99% = .5310511)
sum medDist, detail
hist medDist


*** set vfpDist at 99% constrained
replace medDist = .5310511


*** get  of socecon at min
predict phatmedDist99


*** replace vfpDist with saved backup
replace medDist = medDist_bu


*** gen predicted difference variable
gen medDist_predDiff99 = .
replace medDist_predDiff99 =  phatmedDist99 - phatmednb 


*** look at average number of avoided cases
sum medDist_predDiff99, detail


*** gen observed difference variable
gen medDist_obsDiff99 = .
replace medDist_obsDiff99 = phatmedDist99 - totalGrantedFJC


*** gen total sum var or predictions
gen medDistpred_sum99 = .
replace medDistpred_sum99 = sum(medDist_predDiff99)


*** gen total sum var of obs/pred diff
gen medDistobs_sum99 = .
replace medDistobs_sum99 = sum(medDist_obsDiff99)


*** gen a positive version of preddif
gen pos_medDist_predDiff99 = .
replace pos_medDist_predDiff99 = medDist_predDiff99 * -1


*************************************************************
*** BAR PLOT ESTIMATES FOR CONSTRAINED COUNTERFACTUAL ONE STANDARD DEVIATION BELOW MAX% ***
*************************************************************


est restore mednb



*** set vfpDist at 99% constrained
replace medDist = .1150185


*** get  of socecon at min
predict phatmedDistmean


*** replace vfpDist with saved backup
replace medDist = medDist_bu


*** gen predicted difference variable
gen medDist_predDiffmean = .
replace medDist_predDiffmean =  phatmedDistmean - phatmednb 


*** look at average number of avoided cases
sum medDist_predDiffmean, detail


*** gen observed difference variable
gen medDist_obsDiffmean = .
replace medDist_obsDiffmean = phatmedDistmean - totalGrantedFJC


*** gen total sum var or predictions
gen medDistpred_summean = .
replace medDistpred_summean = sum(medDist_predDiffmean)


*** gen total sum var of obs/pred diff
gen medDistobs_summean = .
replace medDistobs_summean = sum(medDist_obsDiffmean)


*** gen a positive version of preddif
gen pos_medDist_predDiffmean = .
replace pos_medDist_predDiffmean = medDist_predDiffmean * -1


***
gen pos_medDist_predDiffmean2 = .
replace pos_medDist_predDiffmean2 = pos_medDist_predDiffmean if pos_medDist_predDiffmean >= 0
replace pos_medDist_predDiffmean2 = 0 if pos_medDist_predDiffmean <= 0

gen pos_medDist_predDiffmean1 = .
replace pos_medDist_predDiffmean1 = pos_medDist_predDiffmean if pos_medDist_predDiffmean <= 0
replace pos_medDist_predDiffmean1 = 0 if pos_medDist_predDiffmean >= 0


*** save
export excel using "predValuesForStackedBar.xls", firstrow(variables) replace








